﻿
namespace EasyNet.Solr.Test
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Commons;

    class ProductDeserializer : IObjectDeserializer<Product>
    {
        #region IObjectDeserializer<Product> Members

        public IEnumerable<Product> Deserialize(SolrDocumentList result)
        {
            if (result == null)
            {
                yield return null;
            }

            foreach (var doc in result)
            {
                var product = new Product()
                {
                    Id = doc["id"].ToString()
                };

                if (doc.ContainsKey("sku"))
                {
                    product.SKU = Convert.ToString(doc["sku"]);
                }
                if (doc.ContainsKey("name"))
                {
                    product.Name = Convert.ToString(doc["name"]);
                }
                if (doc.ContainsKey("alphaNameSort"))
                {
                    product.AlphaNameSort = Convert.ToString(doc["alphaNameSort"]);
                }
                if (doc.ContainsKey("manu"))
                {
                    product.Manu = Convert.ToString(doc["manu"]);
                }
                if (doc.ContainsKey("cat"))
                {
                    product.Cat = Convert.ToString(doc["cat"]);
                }
                if (doc.ContainsKey("features"))
                {
                    var features = (ArrayList)doc["features"];

                    if (features != null)
                    {
                        product.Features = new string[features.Count];

                        for (var i = 0; i < features.Count; i++)
                        {
                            product.Features[i] = Convert.ToString(features[i]);
                        }
                    }
                }
                if (doc.ContainsKey("includes"))
                {
                    product.Includes = Convert.ToString(doc["includes"]);
                }
                if (doc.ContainsKey("weight"))
                {
                    product.Weight = float.Parse(Convert.ToString(doc["weight"]));
                }
                if (doc.ContainsKey("price"))
                {
                    product.Price = float.Parse(Convert.ToString(doc["price"]));
                }
                if (doc.ContainsKey("popularity"))
                {
                    product.Popularity = Convert.ToInt32(doc["popularity"]);
                }
                if (doc.ContainsKey("inStock"))
                {
                    product.InStock = Convert.ToBoolean(doc["inStock"]);
                }
                if (doc.ContainsKey("store"))
                {
                    product.Store = Convert.ToString(doc["store"]);
                }
                if (doc.ContainsKey("title"))
                {
                    var titles = (ArrayList)doc["title"];

                    if (titles != null)
                    {
                        product.Title = new string[titles.Count];

                        for (var i = 0; i < titles.Count; i++)
                        {
                            product.Title[i] = Convert.ToString(titles[i]);
                        }
                    }
                }
                if (doc.ContainsKey("subject"))
                {
                    product.Subject = Convert.ToString(doc["subject"]);
                }
                if (doc.ContainsKey("description"))
                {
                    product.Description = Convert.ToString(doc["description"]);
                }
                if (doc.ContainsKey("comments"))
                {
                    product.Comments = Convert.ToString(doc["comments"]);
                }
                if (doc.ContainsKey("author"))
                {
                    product.Author = Convert.ToString(doc["author"]);
                }
                if (doc.ContainsKey("keywords"))
                {
                    product.Keywords = Convert.ToString(doc["keywords"]);
                }
                if (doc.ContainsKey("content_type"))
                {
                    var contentTypes = (ArrayList)doc["content_type"];

                    if (contentTypes != null)
                    {
                        product.ContentType = new string[contentTypes.Count];

                        for (var i = 0; i < contentTypes.Count; i++)
                        {
                            product.ContentType[i] = Convert.ToString(contentTypes[i]);
                        }
                    }
                }
                if (doc.ContainsKey("last_modified"))
                {
                    product.LastModified = Convert.ToDateTime(doc["last_modified"]);
                }
                if (doc.ContainsKey("links"))
                {
                    var links = (ArrayList)doc["links"];

                    if (links != null)
                    {
                        product.Links = new string[links.Count];

                        for (var i = 0; i < links.Count; i++)
                        {
                            product.Links[i] = Convert.ToString(links[i]);
                        }
                    }
                }

                yield return product;
            }
        }

        #endregion
    }
}
